/*
 * Copyright (c) 2011 Junichiro Okuno
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Junichiro Okuno.
 */

package tcp_net;

import tcp_net.debug.DebugInfo;
import java.util.List;
import java.util.ArrayList;

/**
	Provides analysis of commands to separate the command part and its arguments
	@author J. Okuno
*/
public class CommandAnalyser
{
	/**
		Debug information
	*/
	private final static boolean DEBUG = DebugInfo.COMMAND_ANALYSER_DEBUG;
	
	/**
		Outputs a command from a text containing a command and arguments
		@param message a text containing a command and arguments
		@return a command name
	*/
	public String getCommand(String message)
	{
		String command = "";
		String[] strs = message.split(" ");

		DebugInfo.print(DEBUG,"Command: "+strs[0]+" Length: "+strs[0].length()); 
		
		return strs[0];
	}
	
	/**
		Outputs a list of arguments from a text containing a command and arguments
		@param message a text containing a command and arguments
		@return a list of arguments
	*/
	public List<String> getArguments(String message)
	{
		List list = new ArrayList<String>();
		
		String[] strs = message.split(" ");
		
		int count = 0;
		for(String s : strs)
		{
			DebugInfo.print(DEBUG, "Message No."+count+": "+s+" Length: "+s.length());
			if(count++ != 0) { list.add(s); }
		}
		
		return list;
	}
	
	/**
		Outputs a substring from a starting char (inclusive) until an ending char (inclusive)
		@param message a string 
		@param start a starting char
		@param end an ending char
		@return a substring
	*/
	public String getSubstring(String message, char start, char end)
	{		
		String substr = "";
		boolean started = false;
		for(int i=0; i < message.length(); i++)
		{
			char ch = message.charAt(i);
			DebugInfo.print(DEBUG, "ch: "+ch+" count: "+i);
			
			if(started)
			{
				if(ch == end) { break; }
				substr += "" + ch;
			}
			
			if(ch == start) 
			{
				if(!started) { started = true; }
			}
		}
		
		DebugInfo.print(DEBUG, "Substring: "+substr);
		
		return substr;
	}		
}